class WishItem < ActiveRecord::Base
  belongs_to :wish_list
  belongs_to :product

  validates_presence_of :wish_list_id
  validates_presence_of :product_id
  validates_uniqueness_of :product_id, :scope => :wish_list_id

  def text
    product.text
  end

  def text= t
    unless self.product
      pt = ProductTag.create!(name: t)
      self.product = Product.create!(:text => t, :product_tag_id => pt.id)
    end

    unless self.product.text == t
      self.product = Product.find_by_text(t).first
    end
  end

end
